﻿using System;
using System.Collections.Generic;
using System.Text;
using Common.IO;
using System.Data.SqlClient;
namespace Common.ADO.Tools
{
    /// <summary>
    /// 将数据表转化成类
    /// </summary>
    public class TableToClass
    {
        /// <summary>
        /// 写入文件
        /// </summary>
        /// <param name="z"></param>
        /// <param name="tableName"></param>
        /// <param name="fileSavePath"></param>
        private void CreateModelClass(List<ZiDuan> z, string tableName, string basepath, string nameSpace)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("using System;");
            sb.AppendLine("namespace " + nameSpace);
            sb.AppendLine("{");
            sb.AppendLine(" [TableAttribute(\"" + tableName + "\")]");
            sb.AppendLine("public class " + tableName);
            sb.AppendLine("{");

            string s = tableName[0].ToString().ToUpper() + tableName.Substring(1);
            for (int i = 0; i < z.Count; i++)
            {
                z[i].Name = z[i].Name[0].ToString().ToUpper() + z[i].Name.Substring(1);
                sb.AppendLine(" [ColumnAttribute(\"" + z[i].Name + "\")]");
                sb.AppendLine("public " + z[i].Type + "  " + z[i].Name + "{get;set;}");
            }
            sb.AppendLine("}");
            sb.AppendLine("}");
            IO.FileDirectoryUtility.CreateDirectory(basepath);
            File.WriteTxt(basepath + s + ".cs", sb.ToString());
        }

        public void WriteClassFromTable(string tableName, string targetDir, string nameSpace)
        {
            string sql = "SELECT * FROM SysColumns WHERE id=Object_Id('" + tableName + "')";
            Write(sql, tableName, targetDir, nameSpace);
        }

        private void Write(string sql, string tableName, string targetDir, string nameSpace)
        {
            List<ZiDuan> cols = new List<ZiDuan>();
            SqlDataReader sdr2 = DbHelperSQL.ExecuteReader(sql);
            while (sdr2.Read())
            {
                ZiDuan z = new ZiDuan();
                z.Name = sdr2["name"].ToString();
                if (sdr2["xtype"].ToString() == "231")
                {
                    z.Type = "string";
                }
                else if (sdr2["xtype"].ToString() == "104")
                {
                    z.Type = "bool";
                }
                else if (sdr2["xtype"].ToString() == "56")
                {
                    z.Type = "int";
                }
                else if (sdr2["xtype"].ToString() == "61")
                {
                    z.Type = "DateTime";
                }
                else if (sdr2["xtype"].ToString() == "99")
                {
                    z.Type = "string";
                }
                else if (sdr2["xtype"].ToString() == "60")
                {
                    z.Type = "decimal";
                }
                else if (sdr2["xtype"].ToString() == "62")
                {
                    z.Type = "float";
                }
                else if (sdr2["xtype"].ToString() == "167")
                {
                    z.Type = "string";
                }
                else if (sdr2["xtype"].ToString() == "175")
                {
                    z.Type = "char";
                }
                else if (sdr2["xtype"].ToString() == "35")
                {
                    z.Type = "string";
                }
                else if (sdr2["xtype"].ToString() == "106")
                {
                    z.Type = "decimal";
                }
                else if (sdr2["xtype"].ToString() == "52")
                {
                    z.Type = "int";
                }
                else if (sdr2["xtype"].ToString() == "108")
                {
                    z.Type = "decimal";
                }
                else
                {
                    //  Response.Write(sdr2["xtype"].ToString() + "<br>");
                }
                cols.Add(z);
            }
            sdr2.Close();

            CreateModelClass(cols, tableName, targetDir, nameSpace);
        }

        /// <summary>
        /// 从数据库反射到类包
        /// </summary>
        /// <param name="dbname">数据库名</param>
        /// <param name="targetDir">目标文件夹 如d:/a/</param>
        /// <param name="nameSpace">命名空间</param>
        public void WriteClassFromDB(string targetDir, string nameSpace)
        {
            string sql = "select * from sysobjects where xtype = 'U' order by name asc";
            SqlDataReader sdr = DbHelperSQL.ExecuteReader(sql);
            List<string> tables = new List<string>();
            while (sdr.Read())
            {
                tables.Add(sdr[0].ToString());
            }
            sdr.Close();

            for (int i = 0; i < tables.Count; i++)
            {
                sql = "SELECT * FROM SysColumns WHERE id=Object_Id('" + tables[i].ToString() + "')";
                Write(sql, tables[i].ToString(), targetDir, nameSpace);
            }
        }

        private class ZiDuan
        {
            public string Name { get; set; }
            public string Type { get; set; }

        }
    }
}
